package com.cnbot.canapp.listener.environment;

import android.util.Log;

import com.cnbot.canapp.listener.base.MultiSerialItem;
import com.cnbot.canapp.utils.ICanCode;
import com.dc.android_can.can.CanDataType;

/**
 * 描述：环境
 * 作者：dc on 2018/6/20 14:09
 * 邮箱：597210600@qq.com
 */
public class EnvironmentItem extends MultiSerialItem<EnvironmentImp> {

	float pm = 0.0f;
	float co = 0.0f;
	float humidity = 0.0f;
	float temp = 0.0f;

	public EnvironmentItem(EnvironmentImp environmentImp) {
		super(environmentImp, ICanCode.BUF_PM_CODE, ICanCode.BUF_TEMPERATURE_CODE, ICanCode.BUF_HUMIDITY_CODE, ICanCode.BUF_CO_CODE);
	}

	@Override
	public void onOrderDispatch(int id) {
		if (id == ICanCode.BUF_ENVIRONMENT_CODE && !canDataType.dir) {
			//            Log.e(TAG, "环境");
			environmentCanRead(canDataType);
		}
	}

	/**
	 * @descriptoin	环境板数据
	 * @author	dc
	 * @param canDataType 接收到的数据结构体
	 * @date 2018/6/13 9:18
	 */
	private void environmentCanRead(CanDataType canDataType) {
		if (mListener == null)
			return;
		// 环境板
		if (canDataType.cmdWord == ICanCode.BUF_PM_CODE) {
			// ww 2018.12.13 pm
			pm = ((canDataType.buf[1] << 8) & 0xff00) | (canDataType.buf[0] & 0x00ff);
			Log.v(TAG, "Pm  = " + pm);
			float p = pm;
			mListener.pmListener(p);
		} else if (canDataType.cmdWord == ICanCode.BUF_CO_CODE) {
			// co
			co = ((canDataType.buf[1] * 256) + canDataType.buf[0]) / 10.0f;
			Log.v(TAG, "co  = " + co);
			float c = co;
			mListener.coListener(c);
		} else if (canDataType.cmdWord == ICanCode.BUF_HUMIDITY_CODE) {
			// 湿度
			humidity = ((canDataType.buf[1] * 256) + canDataType.buf[0]) / 100.0f;
			Log.v(TAG, "humidity  = " + humidity);
			float h = humidity;
			mListener.humidityListener(h);
		} else if (canDataType.cmdWord == ICanCode.BUF_TEMPERATURE_CODE) {
			// 温度
			temp = ((canDataType.buf[1] * 256) + canDataType.buf[0]) / 100.0f;
			Log.v(TAG, "temp  = " + temp);
			float t = temp;
			mListener.tempListener(t);
		}
		// 整体上发
		mListener.environListener(pm, co, humidity, temp);
	}

}
